/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file 
 * \brief Buffer implementation for streaming applications - header
 * \author Antti Maula <antti.maula@tkk.fi>
 */
#ifndef _GIMNETAP_COMMONLIB_BUFFER_H_
#define _GIMNETAP_COMMONLIB_BUFFER_H_

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
#define DEFAULT_PREBUFFER_MAX   1000000

/**
 * Buffer structure for storing arbitrary length strings.
 */
typedef struct _stringbuffer {
  unsigned char *bytebuffer; ///< Databuffer

  unsigned char *data_begin; ///< Pointer to  beginning of the real data
  int data_begin_index; ///< Index to data begin for easier calculations
  int data_length;   ///< Length of data ((end-begin)+1)

  int buffer_length; ///< Allocated memory block size
  int minimum_buffer_length_increment; ///< Minimum increment to do.
  int maximum_buffer_length; ///< Maximum buffer length (to grow to)
  int minimum_buffer_length; ///< Minimum length (never reallocated smaller)

  int maximum_unused_prebuffer; ///< Max. bytes behind data_begin before move.
} TBuffer;


TBuffer *bufferCreate(TBuffer *buffer);
TBuffer *bufferCreateEx(TBuffer *buffer,
                        int initial_size, int minimum_increment,
                        int max_buffer, int max_prebuffer);

void bufferInit(TBuffer *buffer);
void bufferInitEx(TBuffer *buffer, 
                  int initial_size, int minimum_increment,
                  int max_buffer, int max_prebuffer);

void bufferDeinit(TBuffer *buffer);
void bufferDestroy(TBuffer *buffer); 

extern inline int bufferGetLength(const TBuffer *buffer);

void bufferClear(TBuffer *buffer);

void bufferShift(TBuffer *buffer, int shiftBytes);
void bufferAppendSizeHint(TBuffer *buffer, int bytes);
void bufferAppend(TBuffer *buffer, const unsigned char *data, int bytes);
void bufferAppendFromBuffer(TBuffer *buffer,
                            const TBuffer *inbuffer, 
                            int from, int bytes);
extern inline const unsigned char *bufferGetPtr(const TBuffer *buffer);

void bufferSetMinimumIncrement(TBuffer *buffer, int increment);
void bufferSetMaxBufferSize(TBuffer *buffer, int size);
void bufferSetMaxPrebufferSize(TBuffer *buffer, int size);


//*****************************************************************************
//*****************************************************************************
//*****************************************************************************


#endif
